<?php

namespace App\Http\Controllers\Advertisement;

use App\Models\AdvToutiao;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;


class ZhongAnController extends Controller
{

   
    const ADVERTISE_SOURCE_TOUTIAO_GROUP = [
        self::ADVERTISE_SOURCE_TOU_TIAO,
        self::ADVERTISE_SOURCE_YI_MA,
        self::ADVERTISE_SOURCE_ZHONG_AN
    ];   

    const ADVERTISE_SOURCE_TOU_TIAO = 51001;//
    const ADVERTISE_SOURCE_YI_MA = 51002;//
    const ADVERTISE_SOURCE_ZHONG_AN = 51003;
    

    const ADVERTISE_SOURCE_CONFLICT = [
        self::ADVERTISE_SOURCE_TOU_TIAO=>'头条',
        self::ADVERTISE_SOURCE_YI_MA=>'亿玛',
        self::ADVERTISE_SOURCE_ZHONG_AN=>'众安'
    ];
    const DEVICE_TYPE_IMEI = 1;//安卓设备类型 imei
    const DEVICE_TYPE_ANDROID_ID = 2;//安卓设备类型androidid

    const OS_TYPE_ANDROID = 0;//系统类型 android
    const OS_TYPE_IOS = 1;//系统类型 ios

    const OS_TYPE_PARAM = [
        self::OS_TYPE_ANDROID ,
        self::OS_TYPE_IOS ,
    ];

    const USER_STATUS_CLICK = 0;//点击
    const USER_STATUS_ACTIVE = 1;//激活
    const USER_STATUS_REGISTER = 2;//注册
    const USER_STATUS_LOAN_REQUEST = 3;//授信

    const ADVERTISE_PALCE_TYPE_ONE = 1;//
    const ADVERTISE_PALCE_TYPE_TWO = 2;//
    const ADVERTISE_PALCE_TYPE_THREE = 3;//
    const ADVERTISE_PALCE_TYPE_FOUR = 4;//
    const ADVERTISE_PALCE_TYPE_FIVE = 5;//
    const ADVERTISE_PALCE_TYPE_SIX = 6;//
    const ADVERTISE_PALCE_TYPE_SEVEN = 7;//
    const ADVERTISE_PALCE_TYPE_EIGHT = 8;//
    const ADVERTISE_PALCE_TYPE_NINE = 9;//
    const ADVERTISE_PALCE_TYPE_TEN = 10;//

    public function index(Request $request){
        Log::debug('请求参数'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
    }


    public function advertise_zhongan_android(Request $request) {

//        dd($request->input('callback_url'));
//        Log::debug('请求参数'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
        $adid = $request->input('adid','');//广告计划 id 原值
        $cid = $request->input('cid','');//广告创意 id
        $csite = $request->input('csite','');//广告投放位置 1=头条信息流、3=详情页、11=段子 信息流
        $ctype = $request->input('ctype','');//创意样式 2=小图模式 3=大图式 4=组图模式 5=视频
        $idfa = $request->get('idfa','');//iOS IDFA 适用 iOS6及以上,原值（备注：iOS 手机广告唯一标识）
        if(is_null($idfa)) {
            $idfa = '';
        }
        $androidid = $request->input('androidid','');//用户终端的AndroidID，原值（备注：安卓硬件设备唯一标识）
        if(is_null($androidid)) {
            $androidid = '';
        }
        $imei = $request->input('imei','');//用户终端的IMEI,15 位数字
        if(is_null($imei)) {
            $imei = '';
        }
        $os = $request->input('os','');
        $callback_url = $request->input('callback','');
        $timestamp = $request->input('timestamp',time());
        if (!is_int($timestamp)) {
            $timestamp = time();
        }
        try{
            $adv_toutiao = new AdvToutiao();
            if (!empty($imei)) {
                $has_advertise = $adv_toutiao
                    ->where('imei',$imei)
                    ->value('imei');

                if (!empty($has_advertise)) {
                   
                    Log::debug("头条用户转化失败：原因：重复添加，请求参数：".json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                    return json_encode(['status'=>1]);
                }

            }else{
                if (!empty($androidid)) {
                    $has_advertise = $adv_toutiao
                        ->where('androidid',$androidid)
                        ->first();
                    if ($has_advertise) {
                        Log::debug("头条用户转化失败：原因：重复添加，请求参数：".json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                        return json_encode(['status'=>1]);
                    }

                }
            }
            $data = [
                'adid'=>$adid,
                'cid'=>$cid,
                'csite'=>$csite,
                'ctype'=>$ctype,
                'idfa'=>$idfa,
                'androidid'=>$androidid,
                'imei'=>$imei,
                'source'=>self::ADVERTISE_SOURCE_ZHONG_AN,
                'os'=>$os,
                'callback_url'=>$callback_url,
                'user_click_time'=>date('Y-m-d H:i:s',substr($timestamp,0,10)),
            ];

            $adv_toutaio = AdvToutiao::create($data);

        }catch (\Exception $exception) {
            Log::debug("头条用户转化异常：原因：".$exception->getMessage().'请求参数:'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
            return json_encode(['status'=>1,'msg'=>$exception->getMessage()]);
        }
        return json_encode(['status'=>0]);

    }

    public function advertise_zhongan_ios(Request $request) {
        // Log::debug('头条请求参数'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
        $adid = $request->input('adid','');//广告计划 id 原值
        $cid = $request->input('cid','');//广告创意 id
        $csite = $request->input('csite','');//广告投放位置 1=头条信息流、3=详情页、11=段子 信息流
        $ctype = $request->input('ctype','');//⽤户终端的 eth0 接⼝的 MAC 地址 去除分隔符 ":",(采用获取原始值)取md5sum 摘要（备注：入网硬件地址）
        $idfa = $request->input('idfa','');//iOS IDFA 适用 iOS6及以上,原值（备注：iOS 手机广告唯一标识）
        $androidid = $request->input('androidid','');//用户终端的AndroidID，原值（备注：安卓硬件设备唯一标识）
        $imei = $request->input('imei','');//用户终端的IMEI,15 位数字
        $os = $request->input('os','');
        $callback_url = $request->input('callback','');
        $timestamp = $request->input('timestamp',time());
        if (!is_int($timestamp)) {
            $timestamp = time();
        }
        $adv_toutiao = new AdvToutiao();
        if (!empty($idfa)) {
            $has_advertise = $adv_toutiao
                ->where('idfa',$idfa)
                ->first();
            if ($has_advertise) {
                Log::debug("头条用户转化失败：原因：重复添加，请求参数：".json_encode($request->all(),JSON_UNESCAPED_UNICODE));
                return json_encode(['status'=>1]);
            }
        }

        $data = [
            'adid'=>$adid,
            'cid'=>$cid,
            'csite'=>$csite,
            'ctype'=>$ctype,
            'idfa'=>$idfa,
            'androidid'=>$androidid,
            'imei'=>$imei,
            'source'=>self::ADVERTISE_SOURCE_ZHONG_AN,
            'os'=>$os,
            'callback_url'=>$callback_url,
            'user_click_time'=>date('Y-m-d H:i:s',substr($timestamp,0,10)),
        ];
        try{
            $adv_toutaio = AdvToutiao::create($data);

        }catch (\Exception $exception) {
            Log::debug("头条用户转化异常：原因：".$exception->getMessage().'请求参数:'.json_encode($request->all(),JSON_UNESCAPED_UNICODE));
            return json_encode(['status'=>1]);
        }
        return json_encode(['status'=>0]);

    }

}
